package com.tbs.parkingsaleapp.UI.LoginOrSign;

import abstergo.tbs.betterasyc.AsycBase.AsycWorker;
import android.content.Intent;
import android.graphics.*;
import android.os.Bundle;
import android.widget.ImageView;
import androidx.annotation.Nullable;
import com.alibaba.fastjson.JSON;
import com.google.android.material.tabs.TabLayout;
import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.app.AppCompatActivity;
import com.tbs.parkingsaleapp.Http.JsonData.FaceCheckResult;
import com.tbs.parkingsaleapp.Http.Listenner.FaceCheckHttper;
import com.tbs.parkingsaleapp.R;
import com.tbs.parkingsaleapp.SystemManager.LoadToastMananger;
import com.tbs.parkingsaleapp.UI.FaceTakeActivity;
import com.tbs.parkingsaleapp.UI.LoginOrSign.Base.FaceLoginFragment;
import com.tbs.parkingsaleapp.UI.LoginOrSign.Base.SectionsPagerAdapter;
import com.tbs.parkingsaleapp.UI.LoginOrSign.Base.SignFragment;
import com.tbs.parkingsaleapp.databinding.ActivityLoginOrSignBinding;

import java.io.ByteArrayOutputStream;

public class LoginOrSign extends AppCompatActivity{

    private ActivityLoginOrSignBinding binding;
    public final static int R_Sign=0,R_Login=1;
    SectionsPagerAdapter sectionsPagerAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityLoginOrSignBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
         sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
        ViewPager viewPager = binding.viewPager;
        viewPager.setAdapter(sectionsPagerAdapter);
        TabLayout tabs = binding.tabs;
        tabs.setupWithViewPager(viewPager);
    }
    Bitmap adjustPhotoRotation(Bitmap bm, final int orientationDegree)
    {

        Matrix m = new Matrix();
        m.setRotate(orientationDegree, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2);
        return Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), m, true);
    }
    String facedata(Bitmap bitmap)
    {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        int[] arr=new int[baos.size()];
        int i=0;
        for (byte b:baos.toByteArray()
        ) {
            arr[i++]=b& 0xff;
        }
        return  JSON.toJSON(arr).toString();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode== FaceTakeActivity.SUCCESS)
        {
            byte[] arr=data.getByteArrayExtra(FaceTakeActivity.ResultName);
            Bitmap f= BitmapFactory.decodeByteArray(arr,0, arr.length);
            f= adjustPhotoRotation(f,270);
            String facestr=facedata(f);
            AsycWorker<String, FaceCheckResult> facecheck=new AsycWorker<>(1);
            facecheck.setBackgroundAction(new FaceCheckHttper()).setContext(this);
            switch (requestCode)
            {
                case R_Sign:
                   ImageView view= binding.viewPager.findViewById(R.id.face);
                  SignFragment signFragment=  ((SignFragment)sectionsPagerAdapter.fragments[0]);
                  view.setImageBitmap(f);
                  view.setTag(facestr);
                    LoadToastMananger.Show(this,"正在进行\n人脸验证请稍后...",400);
                    facecheck.setResultAction(signFragment);
                    facecheck.Run(view.getTag().toString());
                    break;
                case R_Login:
                    FaceLoginFragment ffg= (FaceLoginFragment) sectionsPagerAdapter.fragments[2];
                    LoadToastMananger.Show(this,"正在进行人脸验证登录",3500);
                    facecheck.setResultAction(ffg);
                    facecheck.Run(facestr);
                    break;
            }
        }


    }
}